package com.mdlib.helper.ui.fragments.sect.money;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.core.util.common.RegularUtils;
import com.ljlib.core.widget.CountdownButton;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.common.MDConstant;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.AppModel;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.UserModel;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

import static com.mdlib.helper.ui.UIHelper.MONEY;

/**
 * 作者：HQY on 16/11/17 09:41
 * 邮箱：hqy_xz@126.com
 */

public class WithdrawalApplyFragment extends BaseTitleFragment {


    @Bind(R.id.tv_balance_money)
    TextView mTvBalanceMoney;

    @Bind(R.id.et_withdraws_money)
    EditText mEtWithdrawsMoney;
    @Bind(R.id.et_withdraws_account_name)
    EditText mEtWithdrawsAccountName;
    @Bind(R.id.et_withdraws_account)
    EditText mEtWithdrawsAccount;
    @Bind(R.id.tv_withdraws_phone)
    TextView mTvWithdrawsPhone;
    @Bind(R.id.et_apply_phone_code)
    EditText mEtApplyPhoneCode;
    @Bind(R.id.cb_withdraws_code)
    CountdownButton mCbWithdrawsCode;
    @Bind(R.id.tv_immediately_pay)
    TextView mTvImmediatelyPay;
    @Bind(R.id.tv_withdraws_poundage)
    TextView mTvWithdrawsPoundage;
    @Bind(R.id.ll_poundage)
    LinearLayout mLlPoundage;
    private String money = "0";
    private String phone;

    public static WithdrawalApplyFragment newInstance(String money) {
        WithdrawalApplyFragment fragment = new WithdrawalApplyFragment();
        Bundle bundle = new Bundle();
        bundle.putString(MONEY, money);
        fragment.setArguments(bundle);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            money = bundle.getString(MONEY);
        }
        phone = UserModel.getInstance().getPhone();

    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_withdrawal_apply;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.balance_withdrawal_apply, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        if (EmptyUtils.isEmpty(money)) {
            money = "0.0";
        }
        if (EmptyUtils.isEmpty(phone)) {
            phone = getActivity().getResources().getString(R.string.apply_phone_tips);
        }
        init();
    }

    private void init() {

        if (!AppModel.getInstance().getAppFrontInfo().isProportionSwitch()) {
            mLlPoundage.setVisibility(View.GONE);
        } else {
            mLlPoundage.setVisibility(View.VISIBLE);
        }

        mTvBalanceMoney.setText("￥" + money);
        mTvWithdrawsPhone.setText(phone);
        mEtWithdrawsMoney.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                Double moneys = Double.valueOf(money);
                if (EmptyUtils.isNotEmpty(String.valueOf(editable))) {
                    Double etmoney = Double.valueOf(String.valueOf(editable));
                    if (etmoney > moneys) {
                        mEtWithdrawsMoney.setText(money);
                    }
                    mTvWithdrawsPoundage.setText(etmoney * AppModel.getInstance().getAppFrontInfo().getProportion() + "元");

                } else {
                    mTvWithdrawsPoundage.setText("0元");
                }
            }
        });

    }


    private void featchCode(String phoneNum, String codeType) {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .featchPhoneCode(phoneNum, codeType)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {

                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));

    }


    @OnClick({R.id.cb_withdraws_code, R.id.tv_immediately_pay})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cb_withdraws_code:
                featchCode(phone, MDConstant.WITHDRAWS);
                break;
            case R.id.tv_immediately_pay:
                String applyMoney = mEtWithdrawsMoney.getText().toString();
                String name = mEtWithdrawsAccountName.getText().toString();
                String account = mEtWithdrawsAccount.getText().toString();
                String code = mEtApplyPhoneCode.getText().toString();
                if (EmptyUtils.isEmpty(applyMoney)) {
                    ToastUtil.showToast(getActivity().getResources().getString(R.string.apply_withdrawal_money_tips));
                    return;
                }
                if (EmptyUtils.isEmpty(name)) {
                    ToastUtil.showToast(getActivity().getResources().getString(R.string.apply_true_name_alipay_tips));
                    return;
                }
                if (EmptyUtils.isEmpty(account)) {
                    ToastUtil.showToast(getActivity().getResources().getString(R.string.apply_alipay_account_tips));
                    return;
                }
                if (EmptyUtils.isEmpty(code)) {
                    ToastUtil.showToast(getActivity().getResources().getString(R.string.apply_phone_code_tips));
                    return;
                }
                if (!RegularUtils.isNumber(phone)) {
                    ToastUtil.showToast(phone);
                    return;
                }
                if (AccountModel.getInstance().getRoleType().equals("user")) {
                    sendApplyWithdraws(applyMoney, name, account, code);
                } else {
                    rushApplyWithdraws(applyMoney, name, account, code);
                }
                break;
        }
    }

    /**
     * 用户提现
     *
     * @param money
     * @param name
     * @param account
     * @param code
     */
    private void sendApplyWithdraws(String money, String name, String account, String code) {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .applyWithdraws(phone, code, name, money, account)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        stopLoadingDialog();
                        removeFragment();
                        ToastUtil.showToast(baseResult.msg);
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                    }
                }));
    }

    /**
     * 跑客提现申请
     *
     * @param money
     * @param name
     * @param account
     * @param code
     */
    private void rushApplyWithdraws(String money, String name, String account, String code) {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .rushApplyWithdraws(phone, code, name, money, account)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        stopLoadingDialog();
                        removeFragment();
                        ToastUtil.showToast(baseResult.msg);
                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                    }
                }));
    }


}
